<!DOCTYPE html>
<html lang="zh_CN" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="utf-8"/>
    <title>文件列表 File List</title>
    <meta name="renderer" content="webkit"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, file-scalable=0"/>
    <link rel="stylesheet" th:href="@{/statics/layui/css/layui.css}" media="all"/>
    <link rel="stylesheet" th:href="@{/statics/style/admin.css}" media="all"/>
    <script th:inline="javascript">
        var aliyunOSSUrl = [[${aliyunOSSUrl}]];
    </script>
</head>
<body>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">文件列表</div>
                <div class="layui-card-body">

                    <table class="layui-hide" id="file-list-table" lay-filter="file-list-filter"></table>

                    <script type="text/html" id="file-list-table-operate-bar">
                        <a class="layui-btn layui-btn-xs"
                           lay-event="view"><i class="layui-icon layui-icon-about"></i>查看</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs"
                           lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
                    </script>

                    <script type="text/html" id="file-list-table-toolbar">
                        <div class="layui-btn-container">
                            <button class="layui-btn layui-btn-sm" id="layuiadmin-upload-useradmin">
                                上传文件
                            </button>
                        </div>
                    </script>

                </div>
            </div>
        </div>
    </div>
</div>


<script th:src="@{/statics/layui/layui.js}"></script>
<script th:inline="none">
    layui.config({
        base: '/statics/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'upload'], function () {
        var $ = layui.$
            , admin = layui.admin
            , table = layui.table
            , upload = layui.upload;

        var fileTable = table.render({
            elem: '#file-list-table'
            , url: '/api/file/aliyun/list'
            , toolbar: '#file-list-table-toolbar'
            , cols: [[
                {field: 'name', title: '文件名', sort: true, templet:function (d) {
                    if(d.type == 'image'){
                        return '<a href="'+ aliyunOSSUrl + d.key+'"><i class="layui-icon layui-icon-picture"></i> '+d.name+'</a>';
                    }else if(d.type == 'video'){
                        return '<a href="'+ aliyunOSSUrl + d.key+'"><i class="layui-icon layui-icon-play"></i> '+d.name+'</a>';
                    }else{
                        return '<a href="'+ aliyunOSSUrl + d.key+'"><i class="layui-icon layui-icon-file"></i> '+d.name+'</a>';
                    }


                }}
                , {field: 'size', width: 150, title: '文件大小', sort: true}
                , {field: 'type', width: 150, title: '类型', sort: true}
                , {field: 'lastModified', width: 180, title: '最后修改时间', sort: true}
                , {width: 150, align: 'center', fixed: 'right', toolbar: '#file-list-table-operate-bar'}
            ]]
        });

        upload.render({
            elem: '#layuiadmin-upload-useradmin'
            , url: '/api/file/aliyun/upload'
            //, accept: 'images'
            , method: 'post'
            //, acceptMime: 'image/*'
            , before: function () {
                layer.load(1,{shade: 0.8});
            }
            , done: function (res) {
                layer.closeAll();
                layer.msg('图片'+ res.data +'已上传!', {icon: 1, time: 1000});
                fileTable.reload();
            }
        });

        //监听行工具事件
        table.on('tool(file-list-filter)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'view':
                    layer.load(1,{shade: 0.8});
                    var url = aliyunOSSUrl + data.key;
                    if(data.type == 'image') {
                        var img = new Image();
                        img.src = url;
                        if (img.complete) {
                            imageOpen(img, data);
                        } else {
                            img.onload = function () {
                                imageOpen(img, data);
                            };
                        }
                    }else if(data.type == 'video'){
                        videoOpen(url);
                    }else{
                        window.open(url);
                    }
                    break;
                case 'del':
                    layer.confirm('真的删除么', function (index) {
                        $.ajax({
                            url: '/api/file/aliyun/remove?key=' + data.key,
                            method: 'delete',
                            contentType: "application/json;charset=UTF-8",
                            dataType: 'JSON',
                            success: function (res) {
                                if (res.code == '0') {
                                    layer.msg('记录已删除!', {icon: 1, time: 1000}, function () {
                                        obj.del();
                                        layer.close(index); //关闭弹层
                                    });
                                } else {
                                    layer.msg(res.msg);
                                }
                            },
                            error: function (err) {
                            }
                        });
                    });
                    break;
            }
        });

        function imageOpen(img,data) {
            layer.closeAll();
            var imgRate = 1;
            if(img.width > 600 || img.height > 400){
                imgRate = img.width / 600 > img.height / 400 ? img.width / 600 : img.height / 400;
            }
            console.log("img.width:"+img.width+",img.height:"+img.height+",img.rate:"+imgRate);
            var imgWidth = img.width / imgRate;
            var imgHeight = img.height / imgRate;
            layer.open({
                type: 1
                , title: false
                , closeBtn: true
                , shade: 0.8
                , area: [imgWidth + 'px', imgHeight + 'px']
                , shadeClose: false
                , content: '<img src="' + aliyunOSSUrl + data.key + '" width="'+ imgWidth +'" height="'+ imgHeight +'"/>'
            });
        }

        function videoOpen(url) {
            url = encodeURI(url);
            var loadstr='<video width="100%" height="100%"  controls="controls" autobuffer="autobuffer"  autoplay="autoplay" loop="loop" style="position:fixed!important;top:0;left:0;" src="'+ url +'"></video></html>';
            layer.open({
                type:1,
                title: false,
                area: ['800px', '600px'],
                shade: 0.8,
                closeBtn: true,
                content: loadstr
            });
        }

    });
</script>
</body>
</html>